package com.gzdh.dishrecognition.ui.dialog;

import java.util.Calendar;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.widget.MyTimePicker;


/**
 * A dialog that prompts the user for the time of day using a {@link MyTimePicker}.
 */
public class MyTimePickerDialog extends AlertDialog implements OnClickListener,
        MyTimePicker.OnTimeChangedListener {

    /**
     * The callback interface used to indicate the user is done filling in
     * the time (they clicked on the 'Set' button).
     */
    public interface OnTimeSetListener {

        /**
         * @param view The view associated with this listener.
         * @param hourOfDay The hour that was set.
         * @param minute The minute that was set.
         */
        void onTimeSet(MyTimePicker view, int hourOfDay, int minute, int seconds);
    }

    private static final String HOUR = "hour";
    private static final String MINUTE = "minute";
    private static final String SECONDS = "seconds";
    private static final String IS_24_HOUR = "is24hour";
    
    private final MyTimePicker mMyTimePicker;
    private final OnTimeSetListener mCallback;
    private final Calendar mCalendar;
    private final java.text.DateFormat mDateFormat;
        
    int mInitialHourOfDay;
    int mInitialMinute;
    int mInitialSeconds;    
    boolean mIs24HourView;

    /**
     * @param context Parent.
     * @param callBack How parent is notified.
     * @param hourOfDay The initial hour.
     * @param minute The initial minute.
     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
     */
    public MyTimePickerDialog(Context context,OnTimeSetListener callBack,
            int hourOfDay, int minute, int seconds, boolean is24HourView) {
        this(context, 0, callBack, hourOfDay, minute, seconds, is24HourView);
    }

    /**
     * @param context Parent.
     * @param theme the theme to apply to this dialog
     * @param callBack How parent is notified.
     * @param hourOfDay The initial hour.
     * @param minute The initial minute.
     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
     */
    public MyTimePickerDialog(Context context, int theme, OnTimeSetListener callBack,
            int hourOfDay, int minute, int seconds, boolean is24HourView) {
        super(context, theme);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mCallback = callBack;
        mInitialHourOfDay = hourOfDay;
        mInitialMinute = minute;
        mInitialSeconds = seconds;
        mIs24HourView = is24HourView;

        mDateFormat = DateFormat.getTimeFormat(context);
        mCalendar = Calendar.getInstance();
        updateTitle(mInitialHourOfDay, mInitialMinute, mInitialSeconds);
        
        setButton("确定", this);
        setButton2("返回", (OnClickListener) null);
        //setIcon(android.R.drawable.ic_dialog_time);
        
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.time_picker_dialog, null);
        setView(view);
        mMyTimePicker = (MyTimePicker) view.findViewById(R.id.timePicker);

        // initialize state
        mMyTimePicker.setCurrentHour(mInitialHourOfDay);
        mMyTimePicker.setCurrentMinute(mInitialMinute);
        mMyTimePicker.setCurrentSecond(mInitialSeconds);
        mMyTimePicker.setIs24HourView(mIs24HourView);
        mMyTimePicker.setOnTimeChangedListener(this);
    }
    
    public void onClick(DialogInterface dialog, int which) {
        if (mCallback != null) {
            mMyTimePicker.clearFocus();
            mCallback.onTimeSet(mMyTimePicker, mMyTimePicker.getCurrentHour(),
                    mMyTimePicker.getCurrentMinute(), mMyTimePicker.getCurrentSeconds());
        }
    }

    public void onTimeChanged(MyTimePicker view, int hourOfDay, int minute, int seconds) {
        updateTitle(hourOfDay, minute, seconds);
    }
    
    public void updateTime(int hourOfDay, int minuteOfHour, int seconds) {
        mMyTimePicker.setCurrentHour(hourOfDay);
        mMyTimePicker.setCurrentMinute(minuteOfHour);
        mMyTimePicker.setCurrentSecond(seconds);
    }
    
    private void updateTitle(int hour, int minute, int seconds) {
        String sHour = String.format("%02d", hour);
        String sMin = String.format("%02d", minute);
        String sSec = String.format("%02d", seconds);
        setTitle(sHour + ":" + sMin + ":" + sSec);
    }
    
    @Override
    public Bundle onSaveInstanceState() {
        Bundle state = super.onSaveInstanceState();
        state.putInt(HOUR, mMyTimePicker.getCurrentHour());
        state.putInt(MINUTE, mMyTimePicker.getCurrentMinute());
        state.putInt(SECONDS, mMyTimePicker.getCurrentSeconds());
        state.putBoolean(IS_24_HOUR, mMyTimePicker.is24HourView());
        return state;
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int hour = savedInstanceState.getInt(HOUR);
        int minute = savedInstanceState.getInt(MINUTE);
        int seconds = savedInstanceState.getInt(SECONDS);
        mMyTimePicker.setCurrentHour(hour);
        mMyTimePicker.setCurrentMinute(minute);
        mMyTimePicker.setCurrentSecond(seconds);
        mMyTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
        mMyTimePicker.setOnTimeChangedListener(this);
        updateTitle(hour, minute, seconds);
    }
}
